//
//  TestFlowButtonViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/10/17.
//  Copyright © 2020 moon. All rights reserved.
//

#import "TestFlowButtonViewController.h"
#import "UIView+MKUIBadgeProtocol.h"
#import "MKUIBadgeView.h"
@interface TestFlowButtonViewController ()
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@property(nonatomic,strong) MKUIFlowLayoutButton *button;
@property(nonatomic,strong) UIView *hitView;
@property(nonatomic,assign) NSInteger badgeCount;
@end

@implementation TestFlowButtonViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"FlowButton" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.class.funcItem.name;
    self.view.backgroundColor = [UIColor whiteColor];
    NSMutableArray *buttons = [[NSMutableArray alloc] init];
    {
        MKUIFlowLayoutButton *button = [[MKUIFlowLayoutButton alloc] initWithContentStyle:(MKUIFlowLayoutButtonContentStyleHorizontal)];
        button.reverseContent = YES;
        button.clipsToBounds = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitle:@"Stop" forState:UIControlStateNormal];
        [button setTitle:@"AutoAuto" forState:UIControlStateSelected];
        
        [button setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Download" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}] forState:UIControlStateNormal];
        [button setAttributedTitle:[[NSAttributedString alloc] initWithString:@"Downloaded" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}] forState:UIControlStateSelected];
        
        [button setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
        
        [button setImage:[UIImage imageNamed:@"app-icon-1-2.png"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"icon_multiselection.png"] forState:UIControlStateSelected];
        
        button.interitemSpacing = 10;
        button.contentInsets = MKUIEdgeInsetsMakeSameEdge(5);
        button.imageSize = CGSizeMake(40, 40);
        button.minHitSize = CGSizeMake(200, 200);
        
        [button addTarget:self action:@selector(__onClick:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderColor = [UIColor grayColor].CGColor;
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = 10;
        
        button.imageView.layer.borderColor = [UIColor grayColor].CGColor;
        button.imageView.layer.borderWidth = 1;
        button.selected = YES;
        [self.view addSubview:button];
        [buttons addObject:button];
        self.button = button;
        //
        self.hitView = [[UIView alloc] init];
        self.hitView.layer.borderColor = [UIColor grayColor].CGColor;
        self.hitView.layer.borderWidth = 2;
        self.hitView.userInteractionEnabled = NO;
        [self.view addSubview:self.hitView];
        
        [self.button mkSetBadge:[@(self.badgeCount) stringValue] animated:NO];
//        MKUIBadgeView *badgeView = (MKUIBadgeView *)self.button.mkBadgeView;
//        badgeView.badgePosition = MKUIBadgeViewPositionTopRight;
//        badgeView.badgeOffset = CGPointMake(-20, 20);
    }
    self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:buttons constraintParam:(MKUIFlowLayoutConstraintParam_V_C_C) contentInsets:UIEdgeInsetsZero interitemSpacing:5];
    
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(__onRefresh:)];
    
    self.navigationItem.rightBarButtonItems = @[refreshItem];
}
- (void)__onRefresh:(id)sender{
//    [self __reloadData];
    UIImage *image = [self.button imageForState:self.button.state];
    if(!image){
        image = [UIImage imageNamed:@"app-icon-1.png"];
    }else{
        image = nil;
    }
    [self.button setImage:image forState:UIControlStateNormal];
    [self.view setNeedsLayout];
}
- (void)__onClick:(UIButton *)button{
    button.selected = !button.selected;
    
    self.badgeCount++;
    if(self.badgeCount%4<2){
        self.button.mkBadgeClass = [MKUIBadgeDotView class];
    }else{
        self.button.mkBadgeClass = [MKUIBadgeTextView class];
    }
    if(self.badgeCount%10==0){
        [self.button mkSetBadge:nil animated:YES];
    }else{
        [self.button mkSetBadge:[@(self.badgeCount) stringValue] animated:YES];
    }
    
    [self.view setNeedsLayout];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.view.bounds;
    CGRect f1 = bounds;
    f1.size.height = 200;
    MKCGRectAlignMidCenterToRect(&f1, bounds);
    self.flowlayout.bounds = f1;
    [self.flowlayout layoutItemsWithResizeItems:YES];
    //
    {
        CGRect f1 = self.button.frame;
        CGRect f2 = f1;
        f2.size = self.button.minHitSize;
        MKCGRectAlignMidCenterToRect(&f2, f1);
        self.hitView.frame = f2;
    }
    [self.button mkRefreshBadge];
}
@end
